#!/usr/bin/env node


//  npm i kolorist   npm i @clack/prompts npm i axios


const { black, green, red, bgCyan, bgMagenta, link, bgBlue, bold, blue } = require('kolorist');

const { to } = require('await-to-js');



const { intro, outro, spinner, multiselect, confirm, isCancel, text, cancel, select } = require('@clack/prompts');
const utils = require('../utils/index');




const main = async function () {
  try {


    intro(bgCyan(black(` yy - myCommit - commit`)));

    // 获取当前目录
    let [err1] = await to(utils.getParse())
    if (err1) {
      return outro(`${red(bold('错误: '))}当前目录必须是一个 Git 存储库！请确保您在正确的目录中运行此脚本，或者初始化一个新的 Git 存储库。`);
    }

    // 取消所有暂存文件
    await utils.getReset()
    // 提交文件
    let [err4, nameData] = await to(utils.getNameonlys());

    if (err4) {
      return outro(`${red(bold('错误: '))}没有检测到文件差异。您的工作目录很干净，无需提交任何更改。`);
    }

    let zcData = nameData.map(item => {
      return { value: item, label: item };
    })

    const answer = await multiselect({
      message: '选择需要进行提交的文件',
      options: zcData,
      required: true,
    });
    if (isCancel(answer)) {
      outro(`${red(bold('警告: '))}提交已取消`);
      return process.exit(0);
    }

    await utils.gitAdd(answer.join(' '))
    // 获取文件差异

    const submissionTyp = await select({
      message: '选择需要进行提交的信息类型',
      options: [
        { value: "✨feat: ", label: "feat: ✨ 新增功能", emoji: ":sparkles:" },
        { value: "🐛fix: ", label: "fix: 🐛 修复缺陷", emoji: ":bug:" },
        { value: "📝docs: ", label: "docs: 📝 文档变更", emoji: ":memo:" },
        { value: "💄style: ", label: "style: 💄 代码格式（不影响功能，例如空格、分号等格式修正）", emoji: ":lipstick:" },
        { value: "♻️refactor: ", label: "refactor: ♻️ 代码重构（不包括 bug 修复、功能新增）", emoji: ":recycle:" },
        { value: "⚡️perf: ", label: "perf: ⚡️ 性能优化", emoji: ":zap:" },
        { value: "✅test: ", label: "test: ✅ 添加疏漏测试或已有测试改动", emoji: ":white_check_mark:" },
        { value: "📦️build: ", label: "build: 📦️ 构建流程、外部依赖变更（如升级 npm 包、修改 webpack 配置等）", emoji: ":package:" },
        { value: "🎡ci: ", label: "ci: 🎡 修改 CI 配置、脚本", emoji: ":ferris_wheel:" },
        { value: "🔨chore: ", label: "chore: 🔨 回滚 commit", emoji: ":hammer:" },
        { value: "⏪️revert: ", label: "revert: ⏪️ 其他修改", emoji: ":rewind:" }
      ]
      ,
      required: false,
    });
    if (isCancel(submissionTyp)) {
      outro(`${red(bold('警告: '))}提交已取消`);
      return process.exit(0);
    }
    const meaning = await text({
      message: '填写需要提交的信息',
      required: true,
      // initialValue: `${submissionTyp}`,

    });
    if (isCancel(meaning)) {
      outro(`${red(bold('警告: '))}提交已取消`);
      process.exit()
    }


    const sss = spinner();

    sss.start(`正在提交`);

    // 提交信息
    await utils.gitCommit(submissionTyp + meaning);

    // 获取最新
    await utils.getPull()
    // 推送
    await utils.gitPush()

    sss.stop(`${submissionTyp + meaning}`);
    // 获取远程地址
    const origin = await utils.getOrigin()

    outro(bgCyan(`✔️ 已成功提交 `));

    console.log(red(bold(link(`查看提交信息-->${origin}`, origin))));
    process.exit()

  } catch (error) {
    utils.submitWdrl()

    outro(`${red(bold('警告: '))}提交已取消`);
    process.exit()
  }
}


module.exports = main;
